import { defineComponent, inject, ref, type Ref, onMounted } from 'vue';
<%_ if (enableTranslation) { _%>
import { useI18n } from 'vue-i18n';
<%_ } _%>
import GatewayService from './gateway.service';

export default defineComponent({
  name: '<%=jhiPrefixCapitalized%>GatewayComponent',
  setup() {
    const gatewayService = inject('gatewayService', () => new GatewayService(), true);

    const gatewayRoutes: Ref<any[]> = ref([]);
    const updatingRoutes = ref(false);

    const refresh = () => {
      updatingRoutes.value = true;
      gatewayService.findAll().then((res) => {
<%_ if (serviceDiscoveryConsul) { _%>
        gatewayRoutes.value = res.data.map(gatewayRoute => {
          gatewayRoute.serviceInstances = gatewayRoute.serviceInstances.map(serviceInstance => {
            if (serviceInstance.healthService && serviceInstance.healthService.checks) {
              if (
                serviceInstance.healthService.checks.filter((check: any) => check.status === 'PASSING').length ===
                serviceInstance.healthService.checks.length
              ) {
                serviceInstance.instanceInfo = { status: 'UP' };
              } else {
                serviceInstance.instanceInfo = { status: 'DOWN'} ;
              }
            }
            return serviceInstance;
          });
          return gatewayRoute;
        });
<%_ } else { _%>
        gatewayRoutes.value = res.data;
<%_ } _%>
        updatingRoutes.value = false;
      });
    }

    onMounted(() => {
      refresh();
    });

    return {
      gatewayRoutes,
      updatingRoutes,
      gatewayService,
      refresh,
<%_ if (enableTranslation) { _%>
      t$: useI18n().t,
<%_ } _%>
    };
  },
});
